package com.app.Controller;

import java.util.ArrayList;

import com.app.Activity.HelloForm1;
import com.app.Activity.frmBieuQuyetMoiRoiNhom;
import com.app.Activity.trucActivity;
import com.app.Activity.frmMainActivity;

import com.app.Activity.frmNotificationMoiVaoNhom;


import com.app.Activity.trucActivity;


import com.app.Activity.frmPhuActivity;
import com.app.Activity.trucActivity;


import android.app.Activity;
import android.app.AlertDialog;

import android.app.AlertDialog.Builder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import android.os.Handler;
import android.util.Log;
import app.com.ModuleRole.R;

public class ActivityController {
	private static ArrayList<ArrayList<String>> arraylist ;
	private static int _countdg = 0;
	private static int _valueAgree = 0;
	private static int _valueDisAgree = 0;
	private static int _count = 0;
	
	private static int _index = 0;
	private static Activity peerActivity=null;
	//public static String mess;
	public static Handler hander=new Handler();
	public static void showGroupInvite(){
		hander.post(new Runnable() {
			
			@Override
			public void run() {
				frmPhuActivity.show("Co nguoi moi ban vao nhom");
				
			}
		});
	}
	public static void showError(){
		frmPhuActivity.show("Bạn ko có quyền này");
	}
	public static void showMoiVaoNhomNotification(String groupName, String msg) {
		frmPhuActivity.contentText=groupName;
		frmPhuActivity.contentTitle=msg;
		hander.post(frmPhuActivity.raMoiVaoNhomNotif);
	}
	public static void setPeerActivity(Activity peerActivity) {
		ActivityController.peerActivity = peerActivity;
	}

	public static void showNotification(String title, String contentTitle, String contentText){
		Intent intent = new Intent(peerActivity, frmNotificationMoiVaoNhom.class);
		intent.putExtra("ID", 1);
		PendingIntent pendingIntent = PendingIntent.getActivity(peerActivity, 0,
				intent, 0);
		NotificationManager notiManager = (NotificationManager) peerActivity.getSystemService(peerActivity.getBaseContext().NOTIFICATION_SERVICE);
		Notification notif = new Notification(R.drawable.ic_launcher,
				title, System.currentTimeMillis());
		notif.setLatestEventInfo(peerActivity, contentTitle, contentText,
				pendingIntent);

		notiManager.notify(1, notif);		

	}
	
	public static void showNhomChoActivity(){
		//Intent i=new Intent()
	}
	//=============================================truc==================================
	public static void ShowMessage(final String message){
		trucActivity.message=message;
		hander.post(trucActivity.showMessage);
	}
	
	//hien thi noi dung chat
	
	public static void ShowChatMessage(final String message,final String address){
		trucActivity.chatMessage=message;
		trucActivity.address=address;
		hander.post(trucActivity.showChatMessage);
	}
	
	public static void DislayKickMember(){
		hander.post(trucActivity.dislayKickMember);
		
	}
	
	public static void ShowFrm2(){
		hander.post(HelloForm1.showFrm2);
	}
	
	public static void ShowFrm1(){
		hander.post(trucActivity.showForm1);
	}
	
	public static void ShowResultVote(){
		hander.post(trucActivity.showResultVoteKick);
	}
	
	public static void FinishDiscussActivity(){
		hander.post(trucActivity.finishAct);
	}
	
	public static void DoEndTime(){
		hander.post(frmBieuQuyetMoiRoiNhom.doEndTime);
	}
	//===============================truc=================================
	
	
	public static void ShowAlert(Context context, String title, String message,int icon){
		AlertDialog.Builder buider=new Builder(context);
		buider.setTitle(title);
		buider.setMessage(message);
		buider.setIcon(icon);
		buider.setNegativeButton("OK", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
		buider.show();
	}
	//====An
	public static void show_Error(){
		trucActivity.show("Ban khong co quyen na y");
	}
	public static void ShowSignOut(){
		Log.d("PEERDROID", "vao show sign out");
		hander.post(trucActivity.raSignout);
	}
	public static void ShowDanhGia(){
		Log.d("PEERDROID", "vao show danh gia");
		hander.post(trucActivity.raShowDanhGia);
	}
	//Ham Tinh ket qua Danh Gia
	public static void CountResultDanhGia(String result){
		Log.d("PEERDROID","Ket qua da nhan duoc: "+result);
		String []s = result.split(";");
		String []d = null;
		ArrayList<String> array;
		_countdg ++;
		if(arraylist == null){
			Log.d("PEERDROID","arraylist null ");
			arraylist= new ArrayList<ArrayList<String>>();
			for(int i=0; i<s.length; i++){
				
				array = new ArrayList<String>();
				Log.d("PEERDROID","s: "+s[i].toString());
				d = s[i].split("/");
				int j = 0;
				while(j<2){
					
					array.add(d[j].toString());
					j++;
				}
				arraylist.add(array);
				
				Log.d("PEERDROID","arraylist get : "+arraylist.get(i));
			}
		}else{
			Log.d("PEERDROID","arraylist not null ");
			for(int i=0; i<s.length; i++){
			
				array = new ArrayList<String>();
				Log.d("PEERDROID","s: "+s[i].toString());
				d = s[i].split("/");
				int j = 0;
				while(j<2){
					
					array.add(d[j].toString());
					j++;
				}
				for( i=0; i<arraylist.size(); i++){
					if(arraylist.get(i).get(0) == array.get(0)){
						int point = Integer.parseInt(arraylist.get(i).get(1));
						int pointnew = Integer.parseInt(array.get(1));
						point += pointnew;
						String p = String.valueOf(point);
						arraylist.get(i).add(1, p);
					}
				}
			}
		}	
	}
	//Ham tinh ket qua Bieu Quyet
	public static void CountResultBieuQuyet(int index, int value ){
		Log.d("PEERDROID","Ket qua bieu quyet nhan duoc: "+ String.valueOf(index)+"-" + String.valueOf(value));
		_count ++;
		_index = index;
		if(value == 0){
			if(_valueDisAgree ==0){
				_valueDisAgree = value;
			}else{
				_valueDisAgree += value;
			}
		}else {
			if(value == 1 && _valueAgree == 0){
				_valueAgree = value;
			}else{
				_valueAgree += value;
			}
		}
	}
	
	public static int get_countdg() {
		return _countdg;
	}
	public static void set_countdg(int _countdg) {
		ActivityController._countdg = _countdg;
	}
	public static ArrayList<ArrayList<String>> getArraylist() {
		return arraylist;
	}
	public static void setArraylist(ArrayList<ArrayList<String>> arraylist) {
		ActivityController.arraylist = arraylist;
	}
	public static int get_valueAgree() {
		return _valueAgree;
	}
	public static void set_valueAgree(int _valueAgree) {
		ActivityController._valueAgree = _valueAgree;
	}
	public static int get_valueDisAgree() {
		return _valueDisAgree;
	}
	public static void set_valueDisAgree(int _valueDisAgree) {
		ActivityController._valueDisAgree = _valueDisAgree;
	}
	public static int get_count() {
		return _count;
	}
	public static void set_count(int _count) {
		ActivityController._count = _count;
	}
	public static int get_index() {
		return _index;
	}
	public static void set_index(int _index) {
		ActivityController._index = _index;
	}
	
	
	
	
}
